﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Task.Util;

namespace Task.DB
{
    public class ConfigEntity
    {
        internal static string DataBaseConnectionString()
        {
            INIFileUtil ini = new INIFileUtil(AppDomain.CurrentDomain.BaseDirectory + "/Configs/config.ini");

            string dbType = ini.IniReadValue("DataBase", "DBType");
            string DbIP = ini.IniReadValue("DataBase", "DbPath");
            string DbPort = ini.IniReadValue("DataBase", "DbPort");
            string DbName = ini.IniReadValue("DataBase", "DbName");
            string DbUser = ini.IniReadValue("DataBase", "DbUser");
            string DbPwd = ini.IniReadValue("DataBase", "DbPwd");
            DbProviderType dbTypeEnum = (DbProviderType)Enum.Parse(typeof(DbProviderType), dbType);
            return GetConnectionString(dbTypeEnum, DbIP, DbPort, DbName, DbUser, DbPwd);
        }

        private static string GetConnectionString(DbProviderType dbType,
           string DbIP,
           string DbPort,
           string DbName,
           string DbUser,
           string DbPwd
           )
        {
            if (dbType.ToString().ToLower() == DbProviderType.SqlServer.ToString().ToLower())
            {
                if (!string.IsNullOrEmpty(DbPort))
                {
                    return string.Format(@"Data Source={0},{4};Initial Catalog={1};User ID={2};Password={3};connect timeout = 30;",
                   DbIP, DbName, DbUser, DbPwd, DbPort);
                }
                return string.Format(@"Data Source={0};Initial Catalog={1};User ID={2};Password={3};connect timeout = 30;",
                    DbIP, DbName, DbUser, DbPwd);
            }
            else if (dbType.ToString().ToLower() == DbProviderType.MySql.ToString().ToLower())
            {
                return string.Format(@"server={0};database={1};uid={2};pwd={3};",
                    DbIP, DbName, DbUser, DbPwd);
            }
            return string.Empty;
        }
    }

    /// <summary>
    /// 数据库类型枚举
    /// </summary>
    public enum DbProviderType : byte
    {
        SqlServer,
        MySql,
        SQLite,
        Oracle,
        ODBC,
        OleDb,
        Firebird,
        PostgreSql,
        DB2,
        Informix,
        SqlServerCe,
    }
}
